<?php

namespace App\Controller;

class WithdrawController extends AppBaseController
{
    private $withdrawLogic;
    private $userLogic;

    public function _initialize()
    {
        parent::_initialize();
        $this->withdrawLogic = A('App/Withdraw', 'Logic');
        $this->userLogic = A('App/User', 'Logic');
    }

    /**
     * 提现申请
     */
    public function apply()
    {
        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];
        $phone = $userInfo['phone'];

        if (empty($phone)) {
            //走绑定手机
            redirect(C("WEB_DOMAIN") . "/User/bindPhone?reback=" . urlencode(__SELF__));
            exit;
        }

        $blanceInfoRes = $this->userLogic->getBlanceInfo($userId);
        if (static::$res->isErr($blanceInfoRes)) {
            $this->sysErr($blanceInfoRes['Message']);
        }
        $blanceInfo = $blanceInfoRes['Result'];
        $blance = $blanceInfo['blance'];

        $this->assign("blance", $blance);
        $this->display();
    }

    /**
     * 申请取现
     */
    public function doApplyWithdraw()
    {
        $name = I('post.name');
        $income = I('post.income');
        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];
        $phone = $userInfo['phone'];
        if (empty($phone)) {
            $this->ajaxReturn(static::$res->err("绑定手机后再进行操作"));
        }

        $res = $this->withdrawLogic->applyWithdraw($userId, $name, $income);
        $this->ajaxReturn($res);
    }

    /**
     * 提现流水列表
     */
    public function applyList()
    {
        $this->display();
    }

    /**
     * 获取取现记录
     */
    public function doGetWithdrawApplyList()
    {
        $page = I('post.page');
        $page = $page ? $page : 1;
        $size = 20;

        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];

        $listRes = $this->withdrawLogic->getApplyList($page, $size, $userId);

        if (!static::$res->isSuc($listRes)) {
            $this->ajaxReturn($listRes);
        }

        $data = $listRes['Result'];
        $list = $data['list'];
        $count = $data['count'];

        if (empty($list)) {
            $this->ajaxReturn(static::$res->err("暂无数据"));
        }

        $countPage = ceil($count / $size);
        $hasNext = $countPage > $page ? true : false;

        $this->assign("list", $list);
        $html = $this->fetch('doGetWithdrawApplyList');

        $res = static::$res->suc("成功", array(
            "html" => $html,
            "list" => $list,
            "count" => $count,
            "hasNext" => $hasNext
        ));
        $this->ajaxReturn($res);
    }

    public function applyDetial()
    {
        $id = I('get.id');

        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];

        $infoRes = $this->withdrawLogic->getApplyDetial($userId, $id);
        if (static::$res->isErr($infoRes)) {
            $this->sysErr($infoRes['Message']);
        }

        $info = $infoRes['Result'];
        if (empty($info)) {
            $this->sysErr("记录不存在");
        }

        $this->assign("info", $info);
        $this->display();
    }
}
